package com.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpCustomerWarehouseMapper;
import com.monitor.dao.ErpMaterielCopyMapper;
import com.monitor.dao.ErpMaterielFlowSummaryMapper;
import com.monitor.dao.ErpMaterielMapper;
import com.monitor.dao.ErpWarehouseMapper;
import com.monitor.entity.ErpCustomer;
import com.monitor.entity.ErpCustomerWarehouse;
import com.monitor.entity.ErpMateriel;
import com.monitor.entity.ErpMaterielCopy;
import com.monitor.service.ErpCustomerWarehouseService;
import com.monitor.util.BlankUtil;
import com.monitor.util.Result;

import org.springframework.expression.spel.ast.BooleanLiteral;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户仓库表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-06
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ErpCustomerWarehouseServiceImpl extends ServiceImpl<ErpCustomerWarehouseMapper, ErpCustomerWarehouse> implements ErpCustomerWarehouseService {
	@Resource
	private ErpCustomerWarehouseMapper erpCustomerWarehouseMapper;
	
	@Resource
	private ErpMaterielFlowSummaryMapper erpMaterielFlowSummaryMapper;
	
	@Resource
	private ErpMaterielMapper erpMaterielMapper;
	
	@Resource
	private ErpWarehouseMapper erpWarehouseMapper;
	
	@Resource
	private ErpMaterielCopyMapper erpMaterielCopyMapper;

	@Override
	public Result<List<Map<String, Object>>> getMaterielszi(String khid,String ckid,String materiel_name,String materiel_brand,String materiel_Specifications,String materiel_model,String materiel_place,String materiel_unit,String typekcorlc) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list=null;
		if(khid!=null&&khid.equals("0")){
			list=erpMaterielMapper.selectByWarehouseIdzi("",ckid, materiel_name, materiel_brand, materiel_Specifications, materiel_model, materiel_place, materiel_unit,typekcorlc);
		}else{
			list=erpMaterielMapper.selectByWarehouseIdzi(" and m.customer_id="+khid+"",ckid, materiel_name, materiel_brand, materiel_Specifications, materiel_model, materiel_place, materiel_unit,typekcorlc);
		}

		for(Map<String, Object> map:list) {
			int num=0;
			if(map.get("num")!=null) {
				num=Integer.parseInt(map.get("num").toString());
			}
			Double price=0.0;
			if(map.get("materiel_storage_price")!=null) {
				price=Double.parseDouble(map.get("materiel_storage_price").toString());
			}
			Double totalPrice=num*price;
			map.put("totalPrice", totalPrice);
		}
		return new  Result<List<Map<String, Object>>>(list,list.size());
	}

	@Override
	public Result<List<Map<String, Object>>> getMateriels_xswx(int ckid,String typestr,int kehuid) {
		// TODO Auto-generated method stub


		List<Map<String, Object>> list=null;
		if(typestr!=null&&typestr!=""){
			list=erpMaterielMapper.selectByWarehouseId_xswx(ckid,"and m.materiel_name in (select name from  erp_specifications where  pid='SF01543141048407')");
		}else{
			if(kehuid==0){
				list=erpMaterielMapper.selectByWarehouseId_xswx(ckid,"");
			}else{
				list=erpMaterielMapper.selectByWarehouseId_xswx(ckid,"and m.customer_id="+kehuid+"");
			}

		}

		for(Map<String, Object> map:list) {
			int num=0;
			if(map.get("num")!=null) {
				num=Integer.parseInt(map.get("num").toString());
			}
			Double price=0.0;
			if(map.get("materiel_storage_price")!=null) {
				price=Double.parseDouble(map.get("materiel_storage_price").toString());
			}
			Double totalPrice=num*price;
			map.put("totalPrice", totalPrice);
		}
		return new  Result<List<Map<String, Object>>>(list,list.size());
	}
//查询客户留存 物料
	@Override
	public Result<List<Map<String, Object>>> getMateriels_xswx_lc(int ckid,int khid) {
		// TODO Auto-generated method stub


		List<Map<String, Object>> list=null;

		list=erpMaterielMapper.getMateriels_xswx_lc(ckid,khid);


		for(Map<String, Object> map:list) {
			int num=0;
			if(map.get("num")!=null) {
				num=Integer.parseInt(map.get("num").toString());
			}
			Double price=0.0;
			if(map.get("materiel_storage_price")!=null) {
				price=Double.parseDouble(map.get("materiel_storage_price").toString());
			}
			Double totalPrice=num*price;
			map.put("totalPrice", totalPrice);
		}
		return new  Result<List<Map<String, Object>>>(list,list.size());
	}

	@Override
	public Result<List<Map<String, Object>>> getMateriels(int custId,String typestr) {
		// TODO Auto-generated method stub

		List<Integer> ids=erpCustomerWarehouseMapper.selectByCustId(custId);
		String str=BlankUtil.listToString(ids);
		List<Map<String, Object>> list=null;
		if(typestr!=null&&typestr!=""){
			list=erpMaterielMapper.selectByWarehouseType(str);
		}else{
			list=erpMaterielMapper.selectByWarehouseId(str);
		}

		for(Map<String, Object> map:list) {
			int num=0;
			if(map.get("num")!=null) {
				num=Integer.parseInt(map.get("num").toString());
			}
			Double price=0.0;
			if(map.get("materiel_storage_price")!=null) {
				price=Double.parseDouble(map.get("materiel_storage_price").toString());
			}
			Double totalPrice=num*price;
			
			String productionId=map.get("materiel_production_id").toString();
			String[] sheng=productionId.split(",");
			String shen="";
			for(String she:sheng) {
				if(she!=null&&!she.equals("")) {
					shen=shen+she+",";
				}
			}
			if(shen.length()>1) {
				shen=shen.substring(0, shen.length()-1);
			}
			map.put("materiel_production_id", shen);
			
			Boolean zhiNeng=Boolean.valueOf(map.get("iszhi_neng").toString());
			if(zhiNeng) {
				Boolean tai=Boolean.valueOf(map.get("istai").toString());
				if(tai) {
					int id=Integer.parseInt(map.get("corresponding_id").toString());
					ErpMaterielCopy mat=erpMaterielCopyMapper.selectById(id);
					if(mat!=null) {
						shen="";
						for(String she:sheng) {
							if(she!=null&&!she.equals("")) {
								shen=shen+"'"+she+"',";
							}
						}
						if(shen.length()>1) {
							shen=shen.substring(0, shen.length()-1);
						}
						ErpMaterielCopy mat2=new ErpMaterielCopy();
						mat2.setMaterielName(map.get("materiel_name").toString());
						mat2.setMaterielBrand(map.get("materiel_brand").toString());
						mat2.setMaterielSpecifications(map.get("materiel_Specifications").toString());
						mat2.setMaterielModel(map.get("materiel_model").toString());
						mat2.setMaterielPlace(map.get("materiel_place").toString());
						mat2.setMaterielState(map.get("materiel_state").toString());
						mat2.setMaterielUnit(map.get("materiel_unit").toString());
						List<Integer> productId=erpMaterielCopyMapper.selectProductionIds(str, mat2, shen);
						String sheng1="";
						for(Integer pro:productId) {
							ErpMaterielCopy mat1=erpMaterielCopyMapper.selectById(pro);
							sheng1=sheng1+mat1.getMaterielProductionId()+",";
						}
					
						mat.setMaterielProductionId(sheng1);
					}
					map.put("chuan", mat);
					if(mat!=null&&mat.getMaterielStoragePrice()!=null) {
						totalPrice=totalPrice+(mat.getMaterielStoragePrice()*mat.getMaterielNumber());
					}
				}
			}
			
			productionId=map.get("materiel_eqcode").toString();
			sheng=productionId.split(",");
			shen="";
			for(String she:sheng) {
				if(she!=null&&!she.equals("")) {
					shen=shen+she+",";
				}
			}
			if(shen.length()>1) {
				shen=shen.substring(0, shen.length()-1);
			}
			map.put("materiel_eqcode", shen);
			
			productionId=map.get("materiel_rfid").toString();
			sheng=productionId.split(",");
			shen="";
			for(String she:sheng) {
				if(she!=null&&!she.equals("")) {
					shen=shen+she+",";
				}
			}
			if(shen.length()>1) {
				shen=shen.substring(0, shen.length()-1);
			}
			map.put("materiel_rfid", shen);
			map.put("totalPrice", totalPrice);
		}
		return new  Result<List<Map<String, Object>>>(list,list.size());
	}



	@Override
	public Result<List<Map<String, Object>>> getMaterielsCk(int ckid) {
		// TODO Auto-generated method stub


		List<Map<String, Object>> list=erpMaterielMapper.selectByWarehouseId(ckid+"");
		for(Map<String, Object> map:list) {
			int num=0;
			if(map.get("num")!=null) {
				num=Integer.parseInt(map.get("num").toString());
			}
			Double price=0.0;
			if(map.get("materiel_storage_price")!=null) {
				price=Double.parseDouble(map.get("materiel_storage_price").toString());
			}
			Double totalPrice=num*price;
			map.put("totalPrice", totalPrice);
		}
		return new  Result<List<Map<String, Object>>>(list,list.size());
	}

	@Override
	public Result<List<Map<String, Object>>> getMaterielsCkqb(int ckid) {
		// TODO Auto-generated method stub


		List<Map<String, Object>> list=erpMaterielMapper.selectByWarehouseIdqb(ckid+"");

		return new  Result<List<Map<String, Object>>>(list,list.size());
	}

	@Override
	public Result<List<Map<String, Object>>> getErrorMateriels(int custId, String proId) {
		// TODO Auto-generated method stub
		List<Integer> ids=erpCustomerWarehouseMapper.selectByCustId(custId);
		String str=BlankUtil.listToString(ids);
		List<Map<String, Object>> list=null;
		String sql="";
		if(proId!=null&&!proId.equals("")) {
			sql=" AND m.materiel_production_id='"+proId+"' ";
		}
		list=erpMaterielMapper.selectErrorByWarehouseId(str,sql);

		for(Map<String, Object> map:list) {
			int num=0;
			if(map.get("materiel_number")!=null) {
				num=Integer.parseInt(map.get("materiel_number").toString());
			}
			Double price=0.0;
			if(map.get("materiel_storage_price")!=null) {
				price=Double.parseDouble(map.get("materiel_storage_price").toString());
			}
			Double totalPrice=num*price;
			Boolean zhiNeng=Boolean.valueOf(map.get("iszhi_neng").toString());
			if(zhiNeng) {
				Boolean tai=Boolean.valueOf(map.get("istai").toString());
				if(tai) {
					int id=Integer.parseInt(map.get("corresponding_id").toString());
					ErpMateriel mat=erpMaterielMapper.selectById(id);
					map.put("chuan", mat);
					if(mat!=null) {
						totalPrice+=mat.getMaterielStoragePrice()*mat.getMaterielNumber();
					}
				}
			}
			map.put("totalPrice", totalPrice);
		}
		return new  Result<List<Map<String, Object>>>(list,list.size());
	}


}
